package com.example.user.myhjjf_app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.user.myhjjf_app.presenter.UserInfo;
import com.example.user.myhjjf_app.view.Activity_Login;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.pnikosis.materialishprogress.ProgressWheel;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    UserInfo userInfo;
    @BindView(R.id.user_name)
    EditText userName;
    @BindView(R.id.password)
    EditText passWord;
    @BindView(R.id.login)
    Button login;
    String username;
    String password;
    private static String USERNAME = "user_name";
    private static String PASSWORD = "password";
    @BindView(R.id.progress_wheel)
    ProgressWheel progressWheel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        userInfo = new UserInfo(this);
        userName.setText(userInfo.getStringInfo(USERNAME));
        passWord.setText(userInfo.getStringInfo(PASSWORD));
    }

    @OnClick(R.id.login)
    public void onViewClicked() {
        username = userName.getText().toString();
        password = passWord.getText().toString();
        if (username.equals("") || password.equals("")) {
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
        } else {
            progressWheel.setVisibility(View.VISIBLE);
            OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/user/login.do"))
                    .tag(this)
                    .params("username", username)
                    .params("password", password)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            try {
                                JSONObject jsonObject = new JSONObject(response.body());
                                String exception = jsonObject.getString("exception");
                                if (exception.equals("com.sun.star.uno.Exception: 用户名或密码错误")) {
                                    Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                                    progressWheel.setVisibility(View.GONE);
                                } else {
                                    userInfo.setUserInfo(USERNAME, username);
                                    userInfo.setUserInfo(PASSWORD, password);
                                    startActivity(new Intent(MainActivity.this, Activity_Login.class));
                                    finish();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            Toast.makeText(MainActivity.this, "服务器未连接", Toast.LENGTH_SHORT).show();
                        }
                    });

        }
    }
}
